package com.yuneec.fly.views;

import android.content.Context;
import android.util.AttributeSet;
import android.widget.CompoundButton;
import android.widget.ToggleButton;

public class SyncToggleButton extends ToggleButton {
    private boolean mByUser = true;

    public SyncToggleButton(Context context, AttributeSet attrs,
                            int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    public SyncToggleButton(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public SyncToggleButton(Context context) {
        super(context);
    }

    public void updateToogleButton(boolean checked) {
        super.setChecked(checked);
    }

    public void syncState(boolean checked) {
        mByUser = false;
        super.setChecked(checked);
    }

    @Override
    public void setChecked(boolean checked) {
        mByUser = true;
        super.setChecked(checked);
    }

    public boolean isCheckedByUser() {
        return mByUser;
    }

    private OnCheckedChangeListener mListener = new OnCheckedChangeListener() {

        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            mUpdateChangeListener.onCheckedChanged(buttonView, isChecked, mByUser);
            mByUser = true;
        }
    };

    private OnUpdateChangeListener mUpdateChangeListener;

    public void setOnUpdateChangeListener(OnUpdateChangeListener updateChangeListener) {
        super.setOnCheckedChangeListener(mListener);
        mUpdateChangeListener = updateChangeListener;
    }

    public interface OnUpdateChangeListener {
        public void onCheckedChanged(CompoundButton buttonView,
                                     boolean isChecked, boolean byUser);
    }

}
